package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.MytravelQueryObject;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    private IDestinationService destinationService;
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        return new PageInfo(travelMapper.selectForList(qo));
    }

    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    public TravelContent getComment(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    public List<Travel> getTravelsViennumTop3(Long id) {
        return travelMapper.selectTravelsViennumTop3(id);
    }

    public Long saveOrUpdate(Travel travel) {
        //摘要
        String content = travel.getTravelContent().getContent();
        if(content.length()>200){
            travel.setSummary(content.substring(0,200));
        }else{
            travel.setSummary(content);
        }
        //发布时间(扩展)
        //最后修改时间
        travel.setLastUpdateTime(new Date());
        if(travel.getId()==null){
            //创建时间
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            //游记内容
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.insert(travelContent);
        }else {
            travelMapper.updateByPrimaryKey(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travelContent);
        }

        return travel.getId();
    }

    public List<Travel> list() {
        return travelMapper.selectAll();
    }


    //根据用户id获取修改时间最新的前5篇文章
    @Override
    public List<Travel> list5ForReleaseTimeByUserId(Long id) {
        List<Travel> travels = travelMapper.list5ForReleaseTimeByUserId(id);
        return travels;
    }

    @Override
    public List<Travel> listAllByUserId(Long id) {
        return travelMapper.listAllByUserId(id);
    }

    @Override
    public PageInfo<Travel> queryAllTravelByUserId(MytravelQueryObject qo) {
        //先获得分页数据
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        PageInfo pageInfo = new PageInfo(travelMapper.queryAllTravelByUserId(qo));
        List<Travel> travels = pageInfo.getList();
        //根据游记获取目的地与区域的面包屑
        List<Destination> toasts = new ArrayList<>();
        for (Travel travel : travels) {
            //toasts
            toasts = destinationService.getToasts(travel.getDest().getId());
            Collections.reverse(toasts);  //列表反序
            travel.setToasts(toasts);
        }
        return pageInfo;
    }


}
